package it.sources.blog.controller;

import com.github.pagehelper.PageInfo;
import it.sources.blog.domain.Article;
import it.sources.blog.domain.query.ArticleQuery;

import it.sources.blog.service.IArticleService;
import it.sources.untile.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService igs;

    @GetMapping
    List<Article> findAll() {
        List<Article> all = igs.findAll();
        return all;

    }
    //查询单个

    /**
     * 查询单个
     *
     * @return
     */
    @GetMapping("{id}")
    Article findOne(@PathVariable("id") Long id) {
        Article one = igs.findOne(id);
        return one;
    }
    //修改

    /**
     * 修改
     *
     * @return
     */
    @PutMapping
    R updete(@RequestBody Article article) {
        try {
            if (article.getId() != null) {
                igs.upDate(article);
            } else {
                igs.add(article);
            }
            return R.success();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }

    }
    //删除

    /**
     * 删除
     *
     * @return
     */
    @DeleteMapping("{id}")
    R delete(@PathVariable("id") Long id) {
        try {
            igs.delet(id);
            return R.success();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }

    }
    @PatchMapping
        //批量删除
    R PathDelete(@RequestBody Long[]id){
        igs.pathDele(id);
        return  R.success();
    }
    //分页查询
    @PostMapping
    PageInfo<Article> queryPage(@RequestBody ArticleQuery articleQuery){
        PageInfo<Article> PageInfo = igs.queryPage(articleQuery);
        return PageInfo;
    }

}
